package com.xyazm.coupon.mq.config;


import com.xyazm.common.constants.RocketMqConstant;
import com.xyazm.common.enums.RocketMqTopicEnum;
import com.xyazm.coupon.mq.consumer.ReleaseCouponListener;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.exception.MQClientException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 消费者配置
 * @author xuyou
 * @date: 2022/6/6 9:54
 */
@Configuration
public class ConsumerConfig {

    @Autowired
    private RocketMQProperties rocketMQProperties;


    /**
     * 释放优惠券消费者
     * @author xuyou
     * @date: 2022/7/1 14:18
     * @param: releaseCouponListener
     * @return: org.apache.rocketmq.client.consumer.DefaultMQPushConsumer
     */
    @Bean("releaseCouponConsumer")
    public DefaultMQPushConsumer releaseInventoryConsumer(ReleaseCouponListener releaseCouponListener)
            throws MQClientException {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(RocketMqConstant.AFTER_SALE_RELEASE_COUPON_TOPIC);
        consumer.setNamesrvAddr(rocketMQProperties.getNameServer());
        consumer.subscribe(RocketMqConstant.AFTER_SALE_RELEASE_COUPON_CONSUMER_GROUP, "*");
        consumer.registerMessageListener(releaseCouponListener);
        consumer.start();
        return consumer;
    }


}
